import urllib.request

import ssl
import xml.etree.ElementTree as ET
import time

ssl._create_default_https_context = ssl._create_stdlib_context

# 定义保存函数
def saveFile(data):
    path = "F:\\pachong\\02_douban.out"
    f = open(path, 'wb')
    f.write(data)
    f.close()


def traverseXml(element):
    #print (len(element))
    if len(element) > 0:
        for child in element:
            #print("********Movie********")
            #print("Title：",child.get("title"))
            for childchild in child:
                #print(childchild.tag,"：",childchild.text)
                if childchild.tag == "numbers":
                    #print(childchild.text)
                    return childchild.text
            #traverseXml(child)
    #else:
    #    print (element.tag, "----", element.attrib)

# 网址
url = "https://api.speedlottery.com/data/racing/last.xml"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/51.0.2704.63 Safari/537.36'}
req = urllib.request.Request(url=url, headers=headers)

res = urllib.request.urlopen(req)

data = res.read()

# 也可以把爬取的内容保存到文件中
#saveFile(data)

#print(data)
tree = ET.ElementTree(ET.fromstring(data))
root = tree.getroot()
data = traverseXml(root)
data = data.split(",")
print(data)
#data = data.decode()
# 打印抓取的内容
#print(data)

# 打印爬取网页的各类信息
#print(type(res))
#print(res.geturl())
#print(res.info())
#print(res.getcode())
def get_kj_data():
    url = "https://api.speedlottery.com/data/racing/last.xml"
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/51.0.2704.63 Safari/537.36'}
    res = urllib.request.urlopen(req)
    data = res.read()
    tree = ET.ElementTree(ET.fromstring(data))
    root = tree.getroot()
    data = traverseXml(root)
    #print(data)
    return data
old_1_data = 1
def process_zj(data):
    global old_1_data
    if old_1_data == data[1]:
if __name__ == '__main__':
    while True:
        time.sleep(60)
        get_kj_data()